package com.funny.stock.user.service;

import com.funny.stock.user.dao.pojo.SysLoginLogDO;
import com.funny.stock.user.dao.repo.SysLoginLogRepo;
import com.funny.stock.user.vo.LoginModeEnum;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * @author pengl
 * @date 2020/7/14
 */
@Component
public class SysLoginLogServiceImpl implements SysLoginLogService {

    @Autowired
    SysLoginLogRepo repo;

//    @Override
//    public SysLoginLogDO save(SysLoginLogDO obj) {
//        return repo.save(obj);
//    }

    private void saveEvent(String username, String ip, Integer type,boolean isLogout) {
        SysLoginLogDO sysLoginLogDO = new SysLoginLogDO();
        sysLoginLogDO.setIp(ip);
        sysLoginLogDO.setLogTime(new Date());
        //1 login 2 logout
        sysLoginLogDO.setType(isLogout ? 2 : 1);
        sysLoginLogDO.setUsername(username);
        LoginModeEnum loginModeEnum = LoginModeEnum.ofType(type);
        if(loginModeEnum != null)
            sysLoginLogDO.setLogMode(loginModeEnum.getType());

        repo.save(sysLoginLogDO);
    }

    @Override
    public void saveLoginEvent(String username, String ip, Integer type) {
        saveEvent(username,ip,type,false);
    }

    @Override
    public void saveLogoutEvent(String username, String ip, Integer type) {
        saveEvent(username,ip,type,true);
    }
}
